package org.boge.sharpdoc.controller;

import org.boge.sharpdoc.bean.Customer;
import org.boge.sharpdoc.bean.LayuiReturnData;
import org.boge.sharpdoc.bean.SendMsg;
import org.boge.sharpdoc.service.SendMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("message")
public class MessageController {
    @Autowired
    private SendMsgService sendMsgService;
    @Autowired
    private HttpSession session;

    @PostMapping("sendToOther")
    @ResponseBody
    public LayuiReturnData sendMesgToOther(SendMsg sendMsg) {
        // 为了不让用户的手机号码直接暴露，接收到的是6倍
        sendMsg.setSender((Customer) session.getAttribute("current_customer"));
        sendMsg.getReceiver().setPhone(sendMsg.getReceiver().getPhone() / 6);
        sendMsgService.addMsg(sendMsg);
        return new LayuiReturnData(0);
    }

    @GetMapping("showMessage")
    public String showMsg(@RequestParam(defaultValue = "1") Integer index, @RequestParam(defaultValue = "5") Integer pageSize, Model model) {
        Customer customer = (Customer) session.getAttribute("current_customer");
        model.addAttribute("mymsgs", sendMsgService.getOnesMsg(index, pageSize, customer.getPhone()));
        return "customer/myMessage";
    }

    @GetMapping("sendMessagePage")
    public String sendMessagePage() {
        return "customer/sendMessage";
    }
}